package redis

import (
	"time"
)

// 缓存短信验证码 缓存10分钟
func RedisSetSmsCode(mobile, code, sale string) error {
	key := "sms:" + mobile + sale
	return RedisDB.Set(key, code, time.Minute*10).Err()
}

// 销毁短信验证码
func RedisDelSmsCode(mobile, sale string) {
	key := "sms:" + mobile + sale
	RedisDB.Del(key)
}

// 获取短信验证码
func RedisGetSmsCode(mobile, sale string) string {
	key := "sms:" + mobile + sale
	return RedisDB.Get(key).Val()
}

// 短信限流
func SmsCodeLimit(mobile string) bool {
	key := "smsLimit:" + mobile
	if RedisDB.Get(key).Val() == "" {
		RedisDB.Set(key, "0", time.Minute*1) //限制一分钟
	}

	if RedisDB.Get(key).Val() < "1" {
		RedisDB.Incr(key) //自增
		return true
	}
	return false
}
